﻿Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Public Class UpdateCore
    Private Shared mConnected As Boolean
    Private Shared mConnectionChecked As Boolean
#Region "Events"
    Shared Event InternetConnectionStateChanged(Connected As Boolean)
    Private Shared Event CheckInternetConnection()
#End Region
    Shared Property RepositoryServer As String
    Shared Property RepositoryFolder As String
    Shared Property VersionFile As String
    Shared Property Description As String
    Shared Property CurrentAppVersion As String
    Shared Property CurrentInstallerVersion As String
    Shared Property CurrentHelpVersion As String
    Public Shared ReadOnly Property Connected(CheckConnection As Boolean) As Boolean
        Get
            If CheckConnection = True Then
                mConnectionChecked = False
                AddHandler CheckInternetConnection, AddressOf ConnectionChecker
                RaiseEvent CheckInternetConnection()
                Do
                    Application.DoEvents()
                Loop Until mConnectionChecked = True
                RemoveHandler CheckInternetConnection, AddressOf ConnectionChecker
            End If
            Return mConnected
        End Get
    End Property
    Public Shared ReadOnly Property RepositoryMainUpdateVersionFile As String
        Get
            Return String.Format("{0}/{1}", FixURLText(RepositoryServer & RepositoryFolder), VersionFile)
        End Get
    End Property
    Shared Sub Reset()
        CurrentAppVersion = "0.0.0.0"
        CurrentInstallerVersion = "0.0.0.0"
        CurrentHelpVersion = "0.0.0.0"
    End Sub
    Shared Function CheckNewerVersion(NVersion As String, CVersion As String) As Boolean
        If NVersion = "" Then Return False
        If CVersion = "" Then Return True

        If CheckVersionNumberFormat(NVersion) = False Then Return False
        If CheckVersionNumberFormat(CVersion) = False Then Return False

        Dim NVer() As String = NVersion.Split(".")
        Dim CVer() As String = CVersion.Split(".")

        If NVer(0) > CVer(0) Then 'Check major version
            Return True
        ElseIf NVer(0) = CVer(0) Then
            If NVer(1) > CVer(1) Then 'Check minor version
                Return True
            ElseIf NVer(1) = CVer(1) Then
                If NVer(2) > CVer(2) Then  'Check revision
                    Return True
                ElseIf NVer(2) = CVer(2) Then
                    If NVer(3) > CVer(3) Then Return True 'Check build
                End If
            End If
        End If

        Return False
    End Function

#Region "Text Functions"
    Shared Function FixURLText(DirectoryName As String) As String
        Dim tString As String = DirectoryName.Trim
        If Strings.Right(tString, 1) = "/" Then tString = Strings.Left(tString, tString.Length - 1)
        If InStr(tString.ToUpper, "HTTP") = 0 And InStr(tString.ToUpper, "FTP") = 0 Then If Strings.Left(tString, 1) <> "/" Then tString = "/" & tString
        Return tString
    End Function
    Shared Function FixFolderText(DirectoryName As String) As String
        Dim tString As String = DirectoryName.Trim
        If Strings.Right(tString, 1) <> "\" Then tString &= "\"
        Return tString
    End Function
    Shared Function CheckVersionNumberFormat(Version As String) As Boolean
        Dim regex As Regex = New Regex("([0-9][0-9]?[0-9]?[.]){3}[0-9][0-9]?[0-9]?")
        Dim match As Match = regex.Match(Version)
        Return match.Success
    End Function
#End Region
#Region "Internet Functions"
    Private Shared Async Sub ConnectionChecker()
        'Request for request
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(New Uri("http://google.com"))
        Dim resp As System.Net.WebResponse
        Try
            resp = Await req.GetResponseAsync
            resp.Close()
            req = Nothing
            RaiseEvent InternetConnectionStateChanged(True)
            mConnected = True
        Catch ex As Exception
            req = Nothing
            RaiseEvent InternetConnectionStateChanged(False)
            mConnected = False
        End Try
        mConnectionChecked = True
    End Sub
#End Region
#Region "Version File"
    Public Class UpdateVersion
        Property Allow As Boolean
        Property CoreVersion As String
        Property CoreIsBeta As Boolean
        Property CoreType As String
        Property InstallerVersion As String
        Property InstallerMD5 As String
        Property HelpVersion As String
        Property HelpMD5 As String
        Sub WriteVersionFile(FileName As String)
            Dim sf As New StripeFile

            sf.Add(Allow)
            sf.Add(CoreVersion)
            sf.Add(CoreIsBeta)
            sf.Add(CoreType)
            sf.Add(InstallerVersion)
            sf.Add(InstallerMD5)
            sf.Add(HelpVersion)
            sf.Add(HelpMD5)
            sf.AddLine()

            sf.SaveFile(FileName, False)
        End Sub
        Sub ReadVersionData(Data As String)
            Using sf As New StripeFile
                sf.ReadDataString(Data)
                Allow = sf.GetLineItems(0, 0)
                CoreVersion = sf.GetLineItems(0, 1)
                CoreIsBeta = sf.GetLineItems(0, 2)
                CoreType = sf.GetLineItems(0, 3)
                InstallerVersion = sf.GetLineItems(0, 4)
                InstallerMD5 = sf.GetLineItems(0, 5)
                HelpVersion = sf.GetLineItems(0, 6)
                HelpMD5 = sf.GetLineItems(0, 7)
            End Using
        End Sub
    End Class
#End Region
#Region "Definition File"
    Shared Sub WriteAppDefinitionFile(Filename As String)
        Using sf As New StripeFile
            sf.Add(RepositoryServer)
            sf.Add(RepositoryFolder)
            sf.Add(VersionFile)
            sf.AddLine()
            sf.SaveFile(Filename, False)
        End Using
    End Sub
    Shared Function ReadAppDefinitionFile(Filename As String) As SAEventArgs
        Using sf As New StripeFile
            Dim tResult = sf.ReadFile(Filename)
            If tResult.Result <> SAEventArgs.Results.Success Then Return tResult
            If sf.LineItemCount > 0 Then
                RepositoryServer = sf.GetLineItems(0, 0)
                RepositoryFolder = sf.GetLineItems(0, 1)
                VersionFile = sf.GetLineItems(0, 2)
            End If
        End Using
        Return New SAEventArgs("", SAEventArgs.Results.Success)
    End Function
#End Region

End Class
